package loanabook;

import gui.AuthenticationForm;
import gui.GUIUtils;
import gui.JFrameConstructDB;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import core.exceptions.DBAccessException;

import db.DBAccessEngine;

public class Program {

	public static void main(String[] args) {
		
		JFrame splashFrame = new JFrame();
		splashFrame.setUndecorated(true);
		splashFrame.setBackground(new Color(0xff, 0xff, 0xff));
		JLabel label = new JLabel();
		ImageIcon img = new ImageIcon(Program.class.getResource("/resources/splash.png"));
		label.setIcon(img);
		splashFrame.add(label);
		
		try {
			splashFrame.setIconImage(ImageIO.read(JFrameConstructDB.class.getResourceAsStream("/resources/icon.png")));
		} catch (Exception e) {
			//do nothing
		}
		
		int width = 350;
	    int height = 300;
	    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
	    int x = (screen.width - width) / 2;
	    int y = (screen.height - height) / 2;
	    splashFrame.setBounds(x, y, width, height);
	    
	    splashFrame.setVisible(true);
	    splashFrame.pack();
	    
	    //wait for connection pool to establish:
	    try {
	    	DBAccessEngine.establishConnectionPool();
	    } catch (Exception e) {
	    	GUIUtils.showMessageError(new DBAccessException("Could not open connection with the database", e), splashFrame);
	    	System.exit(-1);
	    }
	    
	    //once we reached here - all connections have been opened,
	    //close splash and show authentication form:
	    
	    AuthenticationForm.runGUI();
	    splashFrame.dispose();
	}
}
